package com.fsnbigdata.util;

import com.jayway.jsonpath.JsonPath;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by zhongcheng on 13/10/2017.
 */
public class TaxonomyJsonUtils {

    public static List<Naming> readKingdom(String taxonomy){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.type == 'kingdom')]['name', 'latin']");
        return c.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }

    public static List<Naming> readKingdom(String taxonomy, String query){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.type == 'kingdom')]['name', 'latin']");
        List<Map<String, String>> ret = c.stream().filter(it -> it.get("name").contains(query)).collect(Collectors.toList());
        return ret.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }

    public static List<Naming> readPhylumn(String taxonomy, String parent){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.phylumn && @.name == '" + parent + "')].phylumn..[?(@.type == 'phylumn')]['name', 'latin']");
        return c.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }

    public static List<Naming> readPhylumn(String taxonomy, String parent, String query){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.phylumn && @.name == '" + parent + "')].phylumn..[?(@.type == 'phylumn')]['name', 'latin']");
        List<Map<String, String>> ret = c.stream().filter(it -> it.get("name").contains(query)).collect(Collectors.toList());
        return ret.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }

    public static List<Naming> readTClass(String taxonomy, String parent){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.class && @.name == '" + parent + "')].class..[?(@.type == 'class')]['name', 'latin']");
        return c.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }

    public static List<Naming> readTClass(String taxonomy, String parent, String query){
        List<Map<String, String>> c = JsonPath.read(taxonomy, "$..[?(@.class && @.name == '" + parent + "')].class..[?(@.type == 'class')]['name', 'latin']");
        List<Map<String, String>> ret = c.stream().filter(it -> it.get("name").contains(query)).collect(Collectors.toList());
        return ret.stream().map(it -> new Naming(it.get("name"), it.get("latin"))).collect(Collectors.toList());
    }


}
